package hrms.filter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;

public class AMQListerner implements ServletContextListener {
	public Process startProcess;

	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("this is Destroy");

		if (startProcess != null) {
			startProcess.destroy();
			System.out.println("AMQ has destroied !");
		}

	}

	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("this is AMQ Server Init");
		if (!startAMQ()) {
			System.out.println("AMQ Server start faild");
		} else {
			System.out.println("AMQ Server start successed");
		}
	}

	private boolean startAMQ() {
		
		try {
			String mActiveMQRoot = "D:\\apache-activemq-5.15.0";
			System.out.println("Starting ActiveMQ...");
			Runtime runtime = Runtime.getRuntime();
			startProcess = runtime.exec(mActiveMQRoot + "/bin/win64/activemq.bat");
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;

	}
	
	

	public static void main(String[] args) {
	}
}